﻿using chihuopig.IService;
using System;
using System.Collections.Generic;
using System.Text;
using chihuopig.Model;
using System.Threading.Tasks;
using SqlSugar;
using chihuopig.DTO;

using chihuopig.Tool;
using StackExchange.Redis;

namespace chihuopig.Service
{
    public class ArticleService : DBService, IArticleService
    {
        //IMemcachedClient _MemcachedClient;
        //public ArticleService(IMemcachedClient iMemcachedClient)
        //{
        //    _MemcachedClient = iMemcachedClient;
        //}
        IDatabase _iDatabase;
        public ArticleService(IDatabase iDatabase)
        {
            _iDatabase = iDatabase;
        }
        public Task<int> delAll(long[] Id)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            //_MemcachedClient.Remove(CacheHelper.HomeIndexCacheKey);
            return Task.Factory.StartNew<int>(s => 
            {
                string ls = string.Join(",", (long[])s);
                string sql = $"update  Article set IsDelete='1' where id in ({ls})";
                return
                Sqlclient.Ado
               .ExecuteCommand(sql);
            }, Id);

            
        }

        public Task<int> Deleteable(long Id)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            //_MemcachedClient.Remove(CacheHelper.HomeIndexCacheKey);
            return  Sqlclient
                   .Updateable<Article>()
                   .UpdateColumns(it => new Article() { IsDelete = true })
                   .Where(c => c.Id == Id)
                   .ExecuteCommandAsync();
        }

        public Task<Article> EditAsync(long Id)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            //_MemcachedClient.Remove(CacheHelper.HomeIndexCacheKey);
            return Sqlclient.Queryable<Article>().Where(s => s.Id == Id).FirstAsync();
        }

        public Task<ArticleWriterDTO> GetArticle(long ArticleId)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));

            return  Task.Factory.StartNew(a => {
              ArticleWriterDTO mo = new ArticleWriterDTO();

              mo.article=Sqlclient.Queryable<Article>().Where(s => s.Id == (long)a).Single();
              mo.writers = Sqlclient.Queryable<Writer>().ToList();
              return mo;
            }, ArticleId);
           
        }

        public Task<List<Article>> GetPage(int pageIndex, int PageSize)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            return Sqlclient.Queryable<Article>().Where(c => c.IsDelete == false).OrderBy(s => s.Addtime, SqlSugar.OrderByType.Desc).ToPageListAsync(pageIndex, PageSize);
        }

        public Task<double> GetPageTotal(int PageSize)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            return Task.Factory.StartNew<double>(s =>
            {
                double size = Convert.ToDouble(s);
                double bnum = Sqlclient
                .Queryable<Article>()
                .Where(j => j.IsDelete == false && j.IsDelete == false)
                .OrderBy(j => j.Addtime,SqlSugar.OrderByType.Desc).Count() / size;
               
                //Math.Ceiling(bnum);
                return Math.Ceiling(bnum);
            }, PageSize);
        }

        public Task<long> Insertable(Article Model)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            return Sqlclient.Insertable(Model).ExecuteReturnBigIdentityAsync();
        }

        

        public Task<int> Updateable(Article Model)
        {
            _iDatabase.KeyDelete(CacheHelper.HomeIndexCacheKey(1));
            //_MemcachedClient.Remove(CacheHelper.HomeIndexCacheKey);
            return Sqlclient.Updateable<Article>(Model).ExecuteCommandAsync();
        }
    }
}
